package com.tz.system.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tz.system.user.dao.UserRoleDao;
import com.tz.system.user.model.TMgtUser;
import com.tz.system.user.model.TMgtUserRole;
import com.tz.system.user.service.UserRoleService;
/**
 * 
 * @author mike
 *
 */

@Service("userRoleServiceImpl")
@Transactional
public class UserRoleServiceImpl implements UserRoleService{
	
	@Autowired
	private UserRoleDao dao;
	
	//查询所有的用户角色信息
	private static String queryAllHql = "from TMgtUserRole ";
	
	//根据用户ID查询用户的角色配置信息
	private static String queryByUserIdHql = " from TMgtUserRole where userId=? ";
	/**
	 * 查询所有用户
	 */
	public List<TMgtUserRole> queryAll(){
	 	List<TMgtUserRole> userRoleList = dao.find(queryAllHql);
	 	return userRoleList;
	}

	@Override
	public List<TMgtUserRole> queryByUserId(String userId) {
		List<TMgtUserRole> userRoleList = dao.find(queryByUserIdHql, userId);
		return userRoleList;
	}


	@Override
	@Transactional
	public void setUserRole(String userId, String roleCode) {
		List<TMgtUserRole> oldUserRoleList = queryByUserId(userId);
		//需要删除的记录
		List<Integer> removeIds = new ArrayList<Integer>();
		//需要保留的角色记录
		List<String> stillRoles = new ArrayList<String>();
		if(oldUserRoleList!=null && oldUserRoleList.size()> 0 ){
			for(TMgtUserRole ur : oldUserRoleList){
				boolean flag = false;
				//判断当前已存在角色是否已经被删除
				if(roleCode!=null && roleCode.split(",").length>0){
					String roleCodeExsit = ur.getRoleCode() ;
					for(String t : roleCode.split(",")){
						if(roleCodeExsit.equals(t)){
							flag = true;
							stillRoles.add(ur.getRoleCode());
						}
					}
				}
				//如果是旧的角色
				if(!flag){
					removeIds.add(ur.getId());
				}
				
			}
		}
		
	List<TMgtUserRole> insertList = new ArrayList<TMgtUserRole>();
		if(roleCode!=null && roleCode.split(",").length>0){
			for(String role : roleCode.split(",")){
				//新角色
				if(!stillRoles.contains(role)){
					TMgtUserRole entity = new TMgtUserRole();
					entity.setRoleCode(role);
					entity.setUserId(userId);
					insertList.add(entity);
				}
			}
		}
		
		for (Integer id : removeIds) {
			dao.removeById(id);
		}
		
		for (TMgtUserRole entity : insertList) {
			dao.save(entity);
		}
	}
	
	@Override
	@Transactional
	public void removeUserRoleByUserId(String userId){
		List<TMgtUserRole> list = queryByUserId(userId);
		removeUserRoleByList(list);
	}
	
	@Transactional
	public void removeUserRoleByList(List<TMgtUserRole> list) {
		if (list != null && list.size() > 0) {
			for (TMgtUserRole entity : list) {
				this.dao.removeById(entity.getId());
			}
		}
	}
	
	
	@Override
	public List<TMgtUserRole> getUsersByUserIdsAndRole(List<TMgtUser> list, String roleCode) {
		StringBuffer sbf = new StringBuffer("from TMgtUserRole where roleCode=? ");
		String hql = "";
		if(list!=null && list.size()>0){
			sbf.append("and userId in (");
			for(TMgtUser user : list){
				sbf.append("'"+user.getUserId()+"',");
			}
			hql = sbf.toString().substring(0, sbf.toString().length()-1);
			hql = hql + ")";
		}else{
			hql = sbf.toString();
		}
		List<TMgtUserRole> users = dao.find(hql, roleCode);
		if(users==null || users.size()==0){
			return null;
		}
		return users;
	}
	
}
